<?php
/***********************************************************************
 * @package    	IvanEngine
 * @subpackage 	Core
 * @author     	Ivan <ivanzx@msn.com>
 * @created    	2010-05-19
 ***********************************************************************/
if(defined("IVANENGINE_CORE_HELPER"))
	die("Redefinition macro in :".__FILE__."(".__LINE__.")");
define("IVANENGINE_CORE_HELPER", true);

require_once (dirname(__FILE__).'/../Common.php');
require_once (dirname(__FILE__).'/TagHelper.php');
require_once (dirname(__FILE__).'/UrlHelper.php');
require_once (dirname(__FILE__).'/FormHelper.php');
require_once (dirname(__FILE__).'/FileHelper.php');
require_once (dirname(__FILE__).'/AssetHelper.php');
require_once (dirname(__FILE__).'/StringHelper.php');
require_once (dirname(__FILE__).'/IncludeHelper.php');

class CHelper extends CBase 
{
	public function __construct()
	{
		parent::__construct();
	}
	
	public function __destruct()
	{
		parent::__destruct();
	}
	
	/**
    * Strip slashes recursively from array
    *
    * @param  array $value  the value to strip
    *
    * @return array clean value with slashes stripped
    */
	static public function StripSlashes($value)
	{
		return is_array($value) ? 
			array_map(array('CHelper', 'StripSlashes'), $value) : 
			stripslashes($value);
	}
	
    /**
    * Returns an array value for a path.
    *
    * @param array  $values   The values to search
    * @param string $name     The token name
    * @param array  $default  Default if not found
    *
    * @return array
    */
    static public function GetArrayValueForPath($values, $name, $default = null)
    {
	    if (false === $offset = strpos($name, '['))
	    {
	       return isset($values[$name]) ? $values[$name] : $default;
	    }
	
	    if (!isset($values[substr($name, 0, $offset)]))
	    {
	       return $default;
	    }
	
	    $array = $values[substr($name, 0, $offset)];
	
	    while (false !== $pos = strpos($name, '[', $offset))
	    {
	       $end = strpos($name, ']', $pos);
	       if ($end == $pos + 1)
	       {
	           // reached a []
	           if (!is_array($array))
	           {
	               return $default;
	           }
	           break;
	      }
	      else if (!isset($array[substr($name, $pos + 1, $end - $pos - 1)]))
	      {
	           return $default;
	      }
	      else if (is_array($array))
	      {
	           $array = $array[substr($name, $pos + 1, $end - $pos - 1)];
	           $offset = $end;
	      }
	      else
	      {
	           return $default;
	      }
	    }
	
	    return $array;
    }
    
    static public function RemoveArrayValueForPath(&$values, $name, $default = null)
    {
        if (false === $offset = strpos($name, '['))
        {
            if (isset($values[$name]))
            {
                $value = $values[$name];
                unset($values[$name]);

                return $value;
            }else{
                return $default;
            }
        }

        if (!isset($values[substr($name, 0, $offset)]))
        {
            return $default;
        }

        $value = &$values[substr($name, 0, $offset)];

        while (false !== $pos = strpos($name, '[', $offset))
        {
            $end = strpos($name, ']', $pos);
            if ($end == $pos + 1)
            {
                // reached a []
                if (!is_array($value))
                {
                    return $default;
                }
                break;
            }
            else if (!isset($value[substr($name, $pos + 1, $end - $pos - 1)]))
            {
                return $default;
            }
            else if (is_array($value))
            {
                $parent = &$value;
                $key = substr($name, $pos + 1, $end - $pos - 1);
                $value = &$value[$key];
                $offset = $end;
            }
            else
            {
                return $default;
            }
        }

        if ($key)
        {
            unset($parent[$key]);
        }

        return $value;
    }
    
    static public function HasArrayValueForPath($values, $name)
    {
        if (false === $offset = strpos($name, '['))
        {
            return array_key_exists($name, $values);
        }

        if (!isset($values[substr($name, 0, $offset)]))
        {
            return false;
        }

        $array = $values[substr($name, 0, $offset)];
        while (false !== $pos = strpos($name, '[', $offset))
        {
            $end = strpos($name, ']', $pos);
            if ($end == $pos + 1)
            {
                // reached a []
                return is_array($array);
            }
            else if (!isset($array[substr($name, $pos + 1, $end - $pos - 1)]))
            {
                return false;
            }
            else if (is_array($array))
            {
                $array = $array[substr($name, $pos + 1, $end - $pos - 1)];
                $offset = $end;
            }
            else
            {
                return false;
            }
        }

        return true;
    }
}

?>